package com.music.model;

import java.io.File;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 * Class containing all the sounds and methods for load or unloading the library
 * 
 * @author antho
 */
public class Library extends AbstractModel implements Data {

	private ArrayList<Track> trackList;

	// //////////////////////////////////////////////////////////////////////////////////////
	// ////// CONSTRUCTOR ////////
	// //////////////////////////////////////////////////////////////////////////////////////

	/**
	 * Constructor
	 */
	public Library() {
		File lib = new File(Data.dirLib);
		if (lib.exists() && lib.length() != 0) {
			System.out.println("chargement lib");
			this.trackList = Serialization.readXML();
			System.out.println("lib chargée");
		} else {
			this.trackList = new ArrayList<Track>();
			int rep = JOptionPane.showConfirmDialog(null,
					"Librairie vide \ncharger nouvelle librairie à partir de : \n"
							+ Data.dirMusic + " ?", "Chargement de librairie",
					JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
			if (rep == 0) {
				// Files.fileChooser("write");
				listDirectory(Serialization.fileChooser("write"));// Data.dirMusic);
				Serialization.writeXML(this.trackList);
			}
		}
		for (Track t : this.trackList)
			System.out.println(t.toString());
		// notifyObserver(t.toString());
	}

	// //////////////////////////////////////////////////////////////////////////////////////
	// ////// METHODS ////////
	// //////////////////////////////////////////////////////////////////////////////////////

	/**
	 * @param dir
	 *            : directory that we want to scan
	 */
	private void listDirectory(String node) {
		File f = new File(node);
		File[] child = f.listFiles();
		int end;
		for (int i = 0; i < child.length; i++)
			if (child[i].isFile() && !child[i].getName().startsWith(".")) {
				end = child[i].getName().length() - 1;
				while (end != 0 && child[i].getName().charAt(end) != '.')
					end--;
				if (end != -1
						&& Data.allowedType.contains(child[i].getName()
								.toLowerCase().substring(end + 1)))
					this.trackList.add(new Track(child[i].getAbsolutePath()));
			} else if (child[i].isDirectory())
				listDirectory(node + "/" + child[i].getName());
	}

	// //////////////////////////////////////////////////////////////////////////////////////
	// ////// GETTER SETTER ////////
	// //////////////////////////////////////////////////////////////////////////////////////

	// //////////////////////////////////////////////////////////////////////////////////////
	// ////// AFFICHAGE ////////
	// //////////////////////////////////////////////////////////////////////////////////////

	/**
	 * 
	 */
	public void showLibrary() {
		for (Track t : this.trackList)
			notifyObserver(t.toString());
//			System.out.println(t.toString());
	}

	/**
	 * Unimplemented methods of AbstractModel
	 */
	@Override
	public void search(String s, String opt) {
		for (Track t : this.trackList)
			if (t.contains(s, opt))
				notifyObserver(t.toString());
	}
}
